package com.yonghui.springsecurityoauth2demo.config;

import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author he_jiebing@jiuyv.com
 * @create 2024-05-14 14:07
 */
public class CustomAuthorizationCodeServices implements AuthorizationCodeServices {

    private final Map<String, OAuth2Authentication> authorizationCodeStore = new ConcurrentHashMap<>();
    //private final RandomValueStringGenerator generator = new RandomValueStringGenerator();

    @Override
    public String createAuthorizationCode(OAuth2Authentication authentication) {
        String code = UUID.randomUUID().toString().replaceAll("-","");
        System.out.println(">>>>>>>>>>>>>>>>>code:"+code);
        authorizationCodeStore.put(code, authentication);
        return code;
    }

    @Override
    public OAuth2Authentication consumeAuthorizationCode(String code) {
        return authorizationCodeStore.remove(code);
    }
}
